/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Master_Ruang.java
 *
 * Created on Nov 19, 2012, 4:13:20 PM
 */

package rploo_ciayoo;

/**
 *
 * @author Yulius
 */
import Ruang.Ruang;
import Ruang.RuangKontrol;
import Ruang.RuangTableModel;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Master_Ruang extends javax.swing.JInternalFrame {

    /** Creates new form Master_Ruang */
    public Master_Ruang() {
        initComponents();
        lihatSemua();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        idRuangTxt = new javax.swing.JTextField();
        simpanRuangBtn = new javax.swing.JButton();
        deleteRuangBtn = new javax.swing.JButton();
        batalRuangBtn = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabelRuang = new javax.swing.JTable();
        keluarRuangBtn = new javax.swing.JButton();

        jLabel5.setText("Id Ruang");

        simpanRuangBtn.setText("Simpan");
        simpanRuangBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanRuangBtnActionPerformed(evt);
            }
        });

        deleteRuangBtn.setText("Delete");
        deleteRuangBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteRuangBtnActionPerformed(evt);
            }
        });

        batalRuangBtn.setText("Batal");
        batalRuangBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalRuangBtnActionPerformed(evt);
            }
        });

        tabelRuang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Id Ruang"
            }
        ));
        tabelRuang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelRuangMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabelRuang);

        keluarRuangBtn.setText("Keluar");
        keluarRuangBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarRuangBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(372, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(74, 74, 74)
                            .addComponent(idRuangTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addComponent(simpanRuangBtn)
                            .addGap(107, 107, 107)
                            .addComponent(deleteRuangBtn)
                            .addGap(20, 20, 20)
                            .addComponent(batalRuangBtn))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(322, 322, 322)
                            .addComponent(keluarRuangBtn)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(332, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(idRuangTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(simpanRuangBtn)
                        .addComponent(deleteRuangBtn)
                        .addComponent(batalRuangBtn))
                    .addGap(8, 8, 8)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(16, 16, 16)
                    .addComponent(keluarRuangBtn)
                    .addContainerGap(31, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void batalRuangBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalRuangBtnActionPerformed
        // TODO add your handling code here:
        idRuangTxt.setText(null);
}//GEN-LAST:event_batalRuangBtnActionPerformed

    private void keluarRuangBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarRuangBtnActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_keluarRuangBtnActionPerformed

    private void simpanRuangBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanRuangBtnActionPerformed
        Ruang dosenBaru = new Ruang();
        String id = idRuangTxt.getText();
        dosenBaru.setId_ruang(id);


        try {
            RuangKontrol kontrol;
            kontrol = RuangKontrol.getKoneksi();
            kontrol.tambahRuang(dosenBaru);

            JOptionPane.showMessageDialog(this, "Data Tersimpan");
            idRuangTxt.setText("");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan!!! \n Mohon Ulangi");
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }

        lihatSemua();
    }//GEN-LAST:event_simpanRuangBtnActionPerformed

    private void tabelRuangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelRuangMouseClicked
        // TODO add your handling code here:
         int row = tabelRuang.getSelectedRow();
        String kode = tabelRuang.getValueAt(row, 0).toString();
//        String nama = tabelRuang.getValueAt(row, 0).toString();

        idRuangTxt.setText(kode);
        simpanRuangBtn.setEnabled(true);
        
    }//GEN-LAST:event_tabelRuangMouseClicked

    private void deleteRuangBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteRuangBtnActionPerformed
        // TODO add your handling code here:
        Ruang RuangBaru = new Ruang();
        String id = idRuangTxt.getText();
        RuangBaru.setId_ruang(id);


        try {
            RuangKontrol kontrol;
            kontrol = RuangKontrol.getKoneksi();
            kontrol.hapusPrepared(RuangBaru);

            JOptionPane.showMessageDialog(this, "Data Terhapus");
            idRuangTxt.setText("");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Terhapus! \n Mohon Ulangi");
            Logger.getLogger(Master_Ruang.class.getName()).log(Level.SEVERE, null, ex);
        }
        lihatSemua();
    }//GEN-LAST:event_deleteRuangBtnActionPerformed

    public void lihatSemua() {
        idRuangTxt.requestFocus();
        try {
            List<Ruang> ruang = RuangKontrol.getKoneksi().lihatDataRuang();
            RuangTableModel table = new RuangTableModel(ruang);
            tabelRuang.setModel(table);
        } catch (SQLException ex) {
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalRuangBtn;
    private javax.swing.JButton deleteRuangBtn;
    private javax.swing.JTextField idRuangTxt;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton keluarRuangBtn;
    private javax.swing.JButton simpanRuangBtn;
    private javax.swing.JTable tabelRuang;
    // End of variables declaration//GEN-END:variables

}
